package per.javase.jdbc;

import java.sql.*;

//使用preparedstatment完成数据库的查
public class JDBCTest02 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//注册驱动
            conn = DriverManager.getConnection("jdbc:mysql://localhost/bjpowernode","root","19970913Xun");//获取连接
            String sql = "select ename,job,sal from emp1 where sal between ? and ?";
            ps = conn.prepareStatement(sql);//获取数据库操作对象
            ps.setInt(1,5000);
            ps.setInt(2,9999);
            rs = ps.executeQuery();//执行SQL语句
            while(rs.next()){
                String ename = rs.getString(1);
                String job = rs.getString(2);
                int sal = rs.getInt(3);
                System.out.println(ename+","+job+","+sal);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
                if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
        }
    }
}
